<template>
  <div>
    <Card shadow>
        <iview-table
            ref="iviewTable"
            :baseURL="queryModel.baseURL"
            :menuId="dataModel.menuId"
            :columns="dataModel.columns"
            :order="dataModel.order"
            :pageNumber="pageModel.pageNumber"
            :pageSize="pageModel.pageSize"
            :defaultFilters="queryModel.defaultFilters"
            v-on="{changeTotalSize:changeTotalSize,events:events,changeSelect:changeSelect}">
        </iview-table>
        <br>
        <iview-page 
            :totalSize="pageModel.totalSize"
            :pageNumber="pageModel.pageNumber" 
            :pageSize="pageModel.pageSize"
            :pageSizeOpts="pageModel.pageSizeOpts"
            v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
        </iview-page>
        <Modal
            v-model="showConfirm"
            title="操作确认"
            cancel-text="跳过"
            @on-ok="doRefuse"
            @on-cancel="doRefuse"
            width="450">
            <p>
                <Input v-model="denialReason" type="textarea" :autosize="{minRows: 3,maxRows: 5}" placeholder="请填写原因..."></Input>
            </p>
        </Modal>
    </Card>
  </div>
</template>

<script>
import iviewTable from '@/view/common/table/Table.vue';
import iviewPage from '@/view/common/page/Page.vue';
export default {
    components: {
        iviewTable,
        iviewPage
    },
    data () {
        return {
            baseURL: '/user/attendance',
            showConfirm: false,
            selectRows: [],
            denialReason: '',// 拒绝原因
            queryModel: {
                // 网关代理路径(必须)+资源路径(可选)
                baseURL: '/user/relaxation',
                defaultFilters: [
                    {'field': 't1.record_type', 'value1': 2, 'operate': '=='}// 调休记录
                ]
            },
            dataModel: {
                menuId: 720,
                columns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },{
                        title: '申请人',
                        key: 'userName',
                        tableAlias: 't1'
                    },
                    {
                        title: '申请时间(开始)',
                        key: 'leavestarttime',
                        type: 'Date'
                    },{
                        title: '申请时间(结束)',
                        key: 'leaveendtime',
                        type: 'Date'
                    },{
                        title: '调整时间(开始)',
                        key: 'starttime',
                        type: 'Date'
                    },{
                        title: '调整时间(结束)',
                        key: 'endtime',
                        type: 'Date'
                    },
                    {
                        title: '调整原因',
                        key: 'reasonsRest',
                        tableAlias: 't1'
                    },{
                        title: '状态',
                        key: 'status',
                        tableAlias: 't1',
                        render: (h, params) => {
                            let temp = params.row.status
                            if (temp === 0) {
                                temp = '待审批'
                            } else if (temp === 1) {
                                temp = '同意'
                            } else if (temp === 2) {
                                temp = '拒绝'
                            } else if (temp === 3) {
                                temp = '已调休'
                            }
                            return temp
                        }
                    }
                ],
                order: 'creation_time desc',
            },
            pageModel: {
                totalSize: 0,
                pageNumber: 1,
                pageSize: 10,
                pageSizeOpts: [10, 50, 100, 500]
            }
        }
    },
    methods: {
        events (item) {
            if (item.path === 'agree') {
                this.agree()
            } else if (item.path === 'refuse') {
                this.refuse()
            }
        },
        agree () {
            if (this.selectRows.length > 0) {
                let ids = []
                for (let row of this.selectRows) {
                    ids.push(row.id)
                }
                let url = this.baseURL + '/checkAgree'
                this.$ajax.post(url, ids).then((response) => {
                    let {code, data} = response.data
                    if (code === 0) {
                        if (data.length > 1) {
                            this.$Modal.confirm({
                                title: '操作提示',
                                content: '<p>可以审批'+data.length+'条记录</p>',
                                onOk: () => {
                                    this.doAgree(data)
                                }
                            })
                        } else if (data.length === 1) {
                            this.doAgree(data)
                        } else {
                            this.$bus.alert('warning', "没有符合条件的记录")
                            this.selectRows = []
                        }
                    } else {
                        this.$bus.alert('error', "操作异常")
                    }
                })
            } else {
                this.$bus.alert("warning", "请至少选择一条记录")
            }
        },
        doAgree (ids) {
            let url = this.baseURL + '/agree'
            this.$ajax.post(url, ids).then((response) => {
                let {code, msg} = response.data
                if (code === 0) {
                    this.$bus.alert('success', msg)
                    this.refresh()
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        refuse () {
            this.denialReason = ''// 清空已存在数据
            if (this.selectRows.length === 1) {
                let row = this.selectRows[0]
                let url = this.baseURL + '/checkRefuse/' + row.id
                this.$ajax.get(url).then((response) => {
                    let {code, data} = response.data
                    if (code === 0) {
                        if (data) {
                            this.showConfirm = true
                        } else {
                            this.$bus.alert("warning", "没有符合条件的记录")
                            this.selectRows = []
                        }
                    } else {
                        this.$bus.alert('error', "操作异常")
                    }
                })
            } else {
                this.$bus.alert("warning", "请选择一条记录")
            }
        },
        doRefuse () {
            let url = this.baseURL + '/refuse'
            this.$ajax.post(url, {
                id: this.selectRows[0].id,
                denialReason: this.denialReason
            }).then((response) => {
                let {code, msg} = response.data
                if (code === 0) {
                    this.$bus.alert('success', msg)
                    this.refresh()
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        refresh () {
            this.$refs.iviewTable.refresh()
        },
        changeSelect (selection) {
            this.selectRows = selection
        },
        changeTotalSize (totalSize) {
            this.pageModel.totalSize = totalSize
        },
        changePageNumber (pageNumber) {
            this.pageModel.pageNumber = pageNumber
        },
        changePageSize (pageSize) {
            this.pageModel.pageSize = pageSize
        },
        init () {
            this.refresh()
        }
    },
    watch: {
        '$route' (to) {
            if (to.path === '/relaxa') {
                this.init()
            }
        }
    }
}
</script>

<style>

</style>
